// pages/order/car.js

const app = getApp()

let api = require('../../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showcarDemandId: 0,
    showcarLongId: 0,
    showcarTypeId: 0,
    carDemand: [],
    carLong: [],
    carType: []


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this
    that.getSetting();
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight - app.globalData.navHeight
        });
      }
    });
  },
  /***
   * 选择
   */
  choose: function(e) {
    var that = this;
    var data = that.data;
    var key = e.currentTarget.dataset.key;
    var type = e.currentTarget.dataset.type;
   // console.log
    that.setData({
      [type]: key,
    })
  },
  /**
   * 提交
   */
  returnCar: function(e) {
    var that = this;
    var data = that.data;
    var vehicleLen = data.carLong[data.showcarLongId].content;

    var vehicleModel = data.carType[data.showcarTypeId].content;
    var useVehicleType = data.carDemand[data.showcarDemandId].content;
    var pages = getCurrentPages();

    var prevPage = pages[pages.length - 2]; //当前页面的上一个页面

    prevPage.setData({
      vehicleLen: vehicleLen,
      vehicleModel: vehicleModel,
      useVehicleType: useVehicleType
    })
    wx.navigateBack({
      delta: 1,

    })
  },
  
  getSetting: function () {
    var that = this;
    var token = wx.getStorageSync('TOKEN')

    api._get('/setting/get/info/7', '', token).then(res => {
     // console.log(res);
      if (res.code == 200) {
        api._get('/setting/get/info/4', '', token).then(res2 => {
         // console.log(res);
          if (res2.code == 200) {
            api._get('/setting/get/info/5', '', token).then(res3 => {
             // console.log(res);
              if (res3.code == 200) {
                that.setData({
                  carType: res3.result,
                  carLong: res2.result,
                  carDemand: res.result
                })
              } else {
                wx.showToast({
                  title: '网络忙请重试',
                  icon: 'none',
                })
              }
            }).catch(e => {
             // console.log(e)
            })
          }
        }).catch(e => {
         // console.log(e)
        })
      }
    }).catch(e => {
     // console.log(e)
    })
  }
})